package com.lina.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lina.dao.DishDao;
import com.lina.domain.Dish;
import com.lina.service.IDishService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements IDishService {

    @Autowired
    private DishDao dishDao;

    @Override
    public boolean savaBook(Dish dish) {
        return dishDao.insert(dish) > 0;
    }

    @Override
    public boolean modify(Dish dish) {
        return dishDao.updateById(dish) > 0;
    }

    @Override
    public boolean delete(Integer id) {
        return dishDao.deleteById(id) > 0;
    }

    @Override
    public Dish get(Integer id){
        return dishDao.selectById(id);
    }

    @Override
    public IPage<Dish> getPage(int currentPage, int pageSize) {
        IPage<Dish> page = new Page<Dish>(currentPage,pageSize);
        dishDao.selectPage(page,null);
        return page;
    }

    @Override
    public IPage<Dish> getPage(int currentPage, int pageSize, Dish dish) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<Dish>();
        lqw.like(Strings.isNotEmpty(dish.getType()), Dish::getType, dish.getType());
        lqw.like(Strings.isNotEmpty(dish.getName()), Dish::getName, dish.getName());
        lqw.like(Strings.isNotEmpty(dish.getDescription()), Dish::getDescription, dish.getDescription());

        IPage<Dish> page = new Page<Dish>(currentPage,pageSize);
        dishDao.selectPage(page,lqw);
        return page;
    }



}
